//
//  AddEducationVC.m
//  MyProject
//
//  Created by jiaming yan on 2017/9/12.
//  Copyright © 2017年 yanjiaming. All rights reserved.
//

#import "AddEducationVC.h"
#import "TypePickerView.h"
#import "GetResumeByUserIdEduExperienceList.h"
#import "MoreMajorVC.h"
#import "GetMajorListByParamMajorList.h"
#import "GetEducationListRootClass.h"

@interface AddEducationVC ()

@property (weak, nonatomic) IBOutlet UITextField *qualificationsTextField;
@property (weak, nonatomic) IBOutlet UITextField *schoolNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *majorTextFIeld;
@property (weak, nonatomic) IBOutlet UITextField *timeTextfield;
@property (copy, nonatomic) NSString *strMaxEduCode;
@property (copy, nonatomic) NSString *strMajorCode;
@property (weak, nonatomic) IBOutlet UIButton *btnMajorChose;
@property (weak, nonatomic) IBOutlet UIView *viewBtnDeleteBg;

@property (strong, nonatomic) NSArray *arrEdu;
@property (strong, nonatomic) NSArray *arrEduCode;

@end

@implementation AddEducationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"编辑教育经历";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    [button setTitleColor:UIColorFromRGB(0x13bacd) forState:UIControlStateNormal];
    button.titleLabel.font = k15Font;
    button.frame = CGRectMake(0, 0, 40, 40);
    [button addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    
    negativeSpacer.width = -10;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,barItem];
    if (IOS11Later) {
        button.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
    self.arrEdu = @[@"专科",@"本科",@"硕士",@"博士",@"博士以上"];
    self.arrEduCode = @[@"EDUCATION_ZK",@"EDUCATION_XS",@"EDUCATION_SX",@"EDUCATION_BS",@"EDUCATION_BSH"];
    
    if (!self.model) {
        //如果是增加,需要隐藏最底端按钮
        self.viewBtnDeleteBg.hidden = YES;
    }else{
        //否则,需要把原始值付进来
        [self setValues];
    }
    
}



/**
 如果是编辑,需要把原始值付进来
 */
- (void)setValues{
    self.qualificationsTextField.text = self.model.educationName;
    self.schoolNameTextField.text = self.model.school;
    self.majorTextFIeld.text = self.model.major;
    self.timeTextfield.text = [NSString stringWithFormat:@"%zd年毕业",self.model.graduation];
    self.strMaxEduCode = self.model.education;
    self.strMajorCode = self.model.education;
}

-(void)saveButtonClick{
    
    NSString *strAlert = @"";
    
    if (self.schoolNameTextField.text.length ==0 ) {
        strAlert = @"请输入学校";
    }else if (self.majorTextFIeld.text.length == 0){
        strAlert = @"请选择专业";
    }else if (self.timeTextfield.text.length == 0){
        strAlert = @"请选择毕业年份";
    }else if (self.strMaxEduCode.length == 0){
        strAlert = @"请选择学历";
    }
    if (strAlert.length != 0) {
        [CommonUtils showToast:strAlert];
        return;
    }
    NSDictionary *dicModel = [[NSDictionary alloc]initWithObjectsAndKeys:self.schoolNameTextField.text,@"school",self.majorTextFIeld.text,@"major",[self.timeTextfield.text stringByReplacingOccurrencesOfString:@"年毕业" withString:@""],@"graduation" ,self.strMaxEduCode,@"education",nil];
    [self showProgressWithView:self.view animated:YES];
    if (self.model) {
        dicModel = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInteger:self.model.eduExperienceId],@"eduExperienceId",self.schoolNameTextField.text,@"school",self.majorTextFIeld.text,@"major",[self.timeTextfield.text stringByReplacingOccurrencesOfString:@"年毕业" withString:@""],@"graduation",self.strMaxEduCode,@"education",nil];
    }
    [YJMSendRequest postDataWithParameters:dicModel url:@"resume/updateEduExperience" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    

}

- (IBAction)btnDeleteOnClick:(id)sender {
    
    NSDictionary *dicModel = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInteger:self.model.eduExperienceId],@"eduExperienceId",nil];
    [self showProgressWithView:self.view animated:YES];
    
    [YJMSendRequest postDataWithParameters:dicModel url:@"resume/deleteEduExperience" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];

}

- (IBAction)btnMajorChooseOnClick:(id)sender {
    MoreMajorVC *vc = [[MoreMajorVC alloc]init];
    vc.moreMajorSelect = ^(GetMajorListByParamMajorList *model){
        self.strMajorCode = model.majorCode;
        self.majorTextFIeld.text = model.majorName;
    };
    [self.navigationController pushViewController:vc animated:YES];
}


-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    __weak typeof(self) weakSelf = self;
    NSDate* _date = [NSDate date];
    
    /*添加你自己响应代码*/
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy"];
    
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:_date];
    NSMutableArray *yearArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 60; i++) {
        [yearArr addObject:[NSString stringWithFormat:@"%zd",[currentDateStr integerValue] - i]];
        
    }
    if (textField == self.timeTextfield) {
        [self.view endEditing:YES];
        TypePickerView *time = [[TypePickerView alloc]init];
        time.typeArr = yearArr;
        time.selectPicker = ^(NSString *timeStr){
            weakSelf.timeTextfield.text = [NSString stringWithFormat:@"%@年毕业",timeStr];
        };
        [time creatTypePickerView:self.view];
        return NO;
    }else if (textField == self.qualificationsTextField) {
        [self.view endEditing:YES];
        [self.view endEditing:YES];
        TypePickerView *time = [[TypePickerView alloc]init];
        time.typeArr = self.arrEdu;
        time.selectPicker = ^(NSString *eduStr){
            weakSelf.qualificationsTextField.text = eduStr;
            for (NSInteger i=0; i<weakSelf.arrEdu.count; i++) {
                if ([eduStr isEqualToString:weakSelf.arrEdu[i]])
                    self.strMaxEduCode = weakSelf.arrEduCode[i];
            }
        };
        [time creatTypePickerView:self.view];
        return NO;
    }
    
    UIView *tempView = [self.view viewWithTag:909090901];
    [tempView removeFromSuperview];
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
